<template>
	<view class="content">
		<view class="search-box flex-center-between">
			<view class="search-input">
				<input type="text" placeholder="订单编号,商品名称" focus
					v-model.trim="keyword" @confirm="doSearch(false)" class="font32 color333"
					placeholder-class="placeholder-class" confirm-type="search">
				<image @tap="clear" v-show="keyword != ''" src="/static/image/delete.png" mode=""></image>
			</view>
			<view class="search-btn font32" @tap="goBack">取消</view>
		</view>
		<view class="search-keyword" @touchstart="blur">
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length > 0">
					<view class="keyword-list-header flex-center-between">
						<view>历史搜索</view>
						<view><image @tap="oldDelete" src="/static/search/delete.png"></image></view>
					</view>
					<view class="keyword flex-wrap">
						<view v-for="(keyword, index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{ keyword }}</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			keyword: '',
			oldKeywordList: [], // 历史搜索
			isShowKeywordList: false,
		}
	},
	onLoad() {
		this.loadOldKeyword()
	},
	methods: {
		blur() {
			uni.hideKeyboard();
		},
		//加载历史搜索,自动读取本地Storage
		loadOldKeyword() {
			uni.getStorage({
				key: 'orderSearch',
				success: res => {
					var orderSearch = JSON.parse(res.data);
					this.oldKeywordList = orderSearch;
				}
			});
		},
		//清除历史搜索
		oldDelete() {
			uni.showModal({
				content: '确定清除历史搜索记录？',
				success: res => {
					if (res.confirm) {
						console.log('用户点击确定');
						this.oldKeywordList = [];
						uni.removeStorage({
							key: 'orderSearch'
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//执行搜索
		async doSearch(text) {
			if(this.keyword == '' && !text) {
				return 
			}
			let val = text || this.keyword
			this.saveKeyword(val); //保存为历史
			uni.navigateTo({
				url: `/pages/search/searchOrderPage?name=${val}`
			});
		},
		//保存关键字到历史记录
		saveKeyword(keyword) {
			uni.getStorage({
				key: 'orderSearch',
				success: res => {
					console.log(res.data);
					var orderSearch = JSON.parse(res.data);
					var findIndex = orderSearch.indexOf(keyword);
					if (findIndex == -1) {
						orderSearch.unshift(keyword);
					} else {
						orderSearch.splice(findIndex, 1);
						orderSearch.unshift(keyword);
					}
					//最多10个纪录
					orderSearch.length > 10 && orderSearch.pop();
					uni.setStorage({
						key: 'orderSearch',
						data: JSON.stringify(orderSearch)
					});
					this.oldKeywordList = orderSearch; //更新历史搜索
				},
				fail: e => {
					var orderSearch = [keyword];
					uni.setStorage({
						key: 'orderSearch',
						data: JSON.stringify(orderSearch)
					});
					this.oldKeywordList = orderSearch; //更新历史搜索
				}
			});
		},
		// 返回
		goBack() {
			uni.navigateBack({
				delta: 1,
				animationType: 'pop-out',
				animationDuration: 200
			})
		},
		
		// 清空搜索内容
		clear() {
			this.keyword = ''
			this.isShowKeywordList = false
		}
		
	}
}
</script>
<style lang="scss">
page {
	background-color: #fff;
}
view {
	display: block;
}
.search-box {
	width: 100%;
	height: 88rpx;
	padding: 0 30rpx;
	.search-input {
		position: relative;
		input {
			width: 590rpx;
			height: 70rpx;
			letter-spacing: 1rpx;
			border: 0;
			border-radius: 32rpx;
			-webkit-appearance: none;
			-moz-appearance: none;
			appearance: none;
			padding: 0 76rpx 0 26rpx;
			margin: 0;
			background-color: #f5f5f5;
		}
		image {
			position: absolute;
			top: 17rpx;
			right: 20rpx;
			width: 36rpx;
			height: 36rpx;
		}
	}
	
	.placeholder-class {
		color: #9e9e9e;
	}
	
	.search-btn {
		width: 66rpx;
		height: 70rpx;
		line-height: 70rpx;
		color: #A66354;
	}
}
.search-keyword {
	width: 100%;
	height: 100%;
	
	.keyword-box {
		height: calc(100vh - 110upx);
		.keyword-block {
			padding: 30rpx;
			.keyword-list-header {
				width: 100%;
				padding-bottom: 11rpx;
				font-size: 30rpx;
				color: #999;
				
				.change {
					margin-right: 14rpx;
					letter-spacing: 1rpx;
				}
				image {
					width: 40upx;
					height: 40upx;
					vertical-align: middle;
				}
			}
			.keyword {
				width: 100%;
				view {
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 26rpx;
					padding: 0 30rpx;
					margin: 30rpx 30rpx 0 0;
					height: 60rpx;
					font-size: 28rpx;
					background-color: #F7F7F7;
					color: #666;
				}
			}
		}
	}
}
</style>
